package com.newcapec.shiro.controller;

import com.newcapec.shiro.constant.SystemCode;
import com.newcapec.shiro.entity.UserInfo;
import com.newcapec.shiro.service.UsersService;
import com.newcapec.shiro.utils.RestResponse;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class MainController {

    @Resource
    private UsersService usersService;

    @RequestMapping("/")
    public String hello(){

        return "hello";
    }

    @RequestMapping("/login")
    public RestResponse login(@RequestBody UserInfo user){
        UserInfo userInfo = usersService.login(user.getUsername(), user.getPassword());
        if(userInfo != null){
            /**
             * 在非前后端分离的项目中：用户信息放在HttpSession对象中，如果在页面中使用登录用户信息
             * 在模板引擎或jsp中可直接获取
             * 在前后端分离的项目中：需要将用户登录信息响应回前端，
             * 前端需要将用户登录信息缓存到前端的存储（比如：sessionStorage, Vuex）
             */
            return RestResponse.success(userInfo);
        }
        return RestResponse.error(SystemCode.UsernameError.getCode(), SystemCode.UsernameError.getMessage());
    }
    @RequestMapping("/success")
    @RequiresPermissions(value = {"role:find", "edit","user:*"}, logical = Logical.OR)
    public String hello2(){

        return "success";
    }
    @RequestMapping("/index")
    @RequiresRoles("admin")
    public String hello4(){



        return "index";
    }
}
